/*
 * SettingPanel.java
 *
 * Created on 2007年12月15日, 下午8:03
 */
package com.hadeslee.fantasyplayer.setting;

import com.hadeslee.fantasyplayer.util.Config;
import com.hadeslee.fantasyplayer.util.Util;
import com.hadeslee.fantasyplayer.util.Version;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author  hadeslee
 */
public class SettingPanel extends javax.swing.JPanel implements Initable {

    /** Creates new form SettingPanel */
    public SettingPanel() {
        initComponents();
        init();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mixers = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        encoding = new javax.swing.JComboBox();
        autoMini = new javax.swing.JCheckBox();
        showTrayIcon = new javax.swing.JCheckBox();
        showPlayTip = new javax.swing.JCheckBox();
        showTitle = new javax.swing.JCheckBox();
        autoShutDown = new javax.swing.JCheckBox();
        shutDownTime = new javax.swing.JSpinner();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        checkUpdate = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        autoClose = new javax.swing.JCheckBox();
        miniHide = new javax.swing.JCheckBox();

        jLabel1.setText(Config.getResource("SettingPanel.audioDevice")); // NOI18N

        jLabel3.setText(Config.getResource("SettingPanel.lyricEncoding")); // NOI18N

        encoding.setEditable(true);
        encoding.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "GBK", "Big5", "UTF-8", "ISO8859-1" }));

        autoMini.setText(Config.getResource("SettingPanel.autoMinimize")); // NOI18N
        autoMini.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                autoMiniActionPerformed(evt);
            }
        });

        showTrayIcon.setText(Config.getResource("SettingPanel.showTrayIcon")); // NOI18N

        showPlayTip.setText(Config.getResource("SettingPanel.showPlayTip")); // NOI18N

        showTitle.setText(Config.getResource("SettingPanel.showSongTitleOnTaskBar")); // NOI18N
        showTitle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showTitleActionPerformed(evt);
            }
        });

        autoShutDown.setText(Config.getResource("SettingPanel.autoShutDownPC")); // NOI18N
        autoShutDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                autoShutDownActionPerformed(evt);
            }
        });

        shutDownTime.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), null, null, java.util.Calendar.MINUTE));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(Config.getResource("SettingPanel.update"))); // NOI18N

        jLabel2.setText(Config.getResource("SettingPanel.autoCheckUpdate")); // NOI18N

        checkUpdate.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "每天", "每周", "每月", "不检查" }));

        jButton1.setText(Config.getResource("SettingPanel.checkNow")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(checkUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(73, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(checkUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton9.setText(Config.getResource("save")); // NOI18N
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton9);

        jButton10.setText(Config.getResource("reset")); // NOI18N
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton10);

        jButton2.setText(Config.getResource("resetAll")); // NOI18N
        jPanel2.add(jButton2);

        autoClose.setText(Config.getResource("SettingPanel.closeWhenPressSave")); // NOI18N

        miniHide.setText(Config.getResource("SettingPanel.hideWhenMinimize")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(showPlayTip)
                                    .addComponent(autoMini))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(showTitle)
                                    .addComponent(showTrayIcon)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(autoShutDown)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(shutDownTime, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(encoding, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                                        .addComponent(autoClose))
                                    .addComponent(mixers, 0, 307, Short.MAX_VALUE)))
                            .addComponent(miniHide, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(76, 76, 76))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(80, 80, 80))))
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(mixers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(autoClose)
                    .addComponent(encoding, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(autoMini)
                    .addComponent(showTrayIcon))
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(showPlayTip)
                    .addComponent(showTitle))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(autoShutDown)
                    .addComponent(shutDownTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(miniHide)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    private void autoMiniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_autoMiniActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_autoMiniActionPerformed

    public void init() {
        Config config = Config.getConfig();
        List list = config.getMixers();
        if (list != null) {
            mixers.removeAllItems();
            for (Object obj : list) {
                mixers.addItem(obj);
            }
        }
        if (config.getAudioDevice() != null) {
            mixers.setSelectedItem(config.getAudioDevice());
        }
        if (config.getEncoding() != null) {
            encoding.setSelectedItem(config.getEncoding());
        }
        autoClose.setSelected(config.isAutoCloseDialogWhenSave());
        autoMini.setSelected(config.isStartAutoMinimize());
        showTrayIcon.setSelected(config.isShowTrayIcon());
        showPlayTip.setSelected(config.isShowPlayTip());
        showTitle.setSelected(config.isShowTitleInTaskBar());
        autoShutDown.setSelected(config.isAutoShutDown());
        miniHide.setSelected(config.isMiniHide());
        shutDownTime.setValue(config.getShutDownTime() != null ? config.getShutDownTime() : new Date());
        shutDownTime.setEnabled(config.isAutoShutDown());
        String strategy = config.getCheckUpdateStrategy();
        if (strategy != null) {
            checkUpdate.setSelectedItem(Config.getResource(strategy));
        }
    }

    private void autoShutDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_autoShutDownActionPerformed
        // TODO add your handling code here:
        shutDownTime.setEnabled(autoShutDown.isSelected());
    }//GEN-LAST:event_autoShutDownActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        // TODO add your handling code here:
        init();
    }//GEN-LAST:event_jButton10ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:
        doSave();
    }//GEN-LAST:event_jButton9ActionPerformed

    private void showTitleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showTitleActionPerformed
        // TODO add your handling code here:
        Config.getConfig().setShowTitleInTaskBar(showTitle.isSelected());
        Config.getConfig().getPlayer().setShowTile(showTitle.isSelected());
    }//GEN-LAST:event_showTitleActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        jButton1.setEnabled(false);
        new Thread() {

            public void run() {
                Version remote = Util.getRemoteVersion();
                Util.checkUpdate(remote,false);
                jButton1.setEnabled(true);
            }
        }.start();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void doSave() {
        Config config = Config.getConfig();
        boolean isAutoShutDown = autoShutDown.isSelected();
        if (isAutoShutDown) {
            try {
                Date shutdownTime = (Date) shutDownTime.getValue();
                config.setShutDownTime(shutdownTime);
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this, Config.getResource("SettingPanel.inputInvalidDateFormat"));
                return;
            }
        }
        String mix = mixers.getSelectedItem().toString();
        String encode = encoding.getSelectedItem().toString();
        boolean isStartAutoMini = autoMini.isSelected();
        boolean isShowTrayIcon = showTrayIcon.isSelected();
        boolean isShowPlayTip = showPlayTip.isSelected();
        boolean isShowTitle = showTitle.isSelected();

        String check = getCheckString(checkUpdate.getSelectedItem().toString());
        config.setAudioDevice(mix);
        config.setEncoding(encode);
        config.setStartAutoMinimize(isStartAutoMini);
        config.setShowPlayTip(isShowPlayTip);
        config.setShowTrayIcon(isShowTrayIcon);
        config.setShowTitleInTaskBar(isShowTitle);
        config.setMiniHide(miniHide.isSelected());
        config.setAutoShutDown(isAutoShutDown);
        config.setAutoCloseDialogWhenSave(autoClose.isSelected());
        config.setCheckUpdateStrategy(check);
        if (config.isAutoCloseDialogWhenSave()) {
            config.getOptionDialog().setVisible(false);
        }
    }

    private String getCheckString(String s) {
        String[] ss = {Config.CHECK_DAY, Config.CHECK_MONTH, Config.CHECK_NONE, Config.CHECK_WEEK};
        for (String temp : ss) {
            if (Config.getResource(temp).equals(s)) {
                return temp;
            }
        }
        return Config.CHECK_NONE;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox autoClose;
    private javax.swing.JCheckBox autoMini;
    private javax.swing.JCheckBox autoShutDown;
    private javax.swing.JComboBox checkUpdate;
    private javax.swing.JComboBox encoding;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JCheckBox miniHide;
    private javax.swing.JComboBox mixers;
    private javax.swing.JCheckBox showPlayTip;
    private javax.swing.JCheckBox showTitle;
    private javax.swing.JCheckBox showTrayIcon;
    private javax.swing.JSpinner shutDownTime;
    // End of variables declaration//GEN-END:variables
}
